package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfInvoiceInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfInvoiceInfo;
import com.dhcc.bpm.modules.scf.service.IScfInvoiceInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfInvoiceInfoVO.ScfGetInvoiceDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfInvoiceInfoVO.ScfGetInvoiceListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 发票信息表 控制器
 *
 * @author liuguoting
 * @since 2023-09-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/")
@Api(value = "发票信息表", tags = "发票信息表接口")
public class ScfInvoiceInfoController extends BladeController {

    private IScfInvoiceInfoService scfInvoiceInfoService;


    /**
     * 分页查询发票信息列表
     */
    @GetMapping("/scfGetInvoiceList")
    // @Cacheable(value = "scfInvoiceListCache", key = "'scfInvoiceListCacheKey-' + #scfInvoiceInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetInvoiceListVO>> page(ScfInvoiceInfoDTO scfInvoiceInfo, Query query) {

        if (StringUtils.isEmpty( scfInvoiceInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfInvoiceInfo.setOrgCodes(  scfInvoiceInfo.getOrgCode().split(",") );


        if(scfInvoiceInfo.getOrgCode().contains( "50000000")){
            scfInvoiceInfo.setOrgCode("50000000");
            scfInvoiceInfo.setOrgCodes("50000000".split(",") );
        }


        IPage<ScfGetInvoiceListVO> pages = scfInvoiceInfoService.selectScfInvoiceInfoPage(Condition.getPage(query), scfInvoiceInfo);
        return R.data(pages);
    }

    /**
     * 完税-查询发票信息详情
     */
    @GetMapping("/scfGetInvoiceDetails")
    // @Cacheable(value = "scfInvoiceDetailsCache", key = "'scfInvoiceDetailsCacheKey-' + #scfInvoiceInfo.rowId")
    public R<ScfGetInvoiceDetailsVO> detail(ScfInvoiceInfoDTO scfInvoiceInfo) {
        return R.data(scfInvoiceInfoService.details(scfInvoiceInfo));
    }

    /**
     * 新增发票信息详情
     */
    @PostMapping("/scfAddInvoiceDetails")
    // @CacheEvict(value = "scfInvoiceListCache", allEntries = true)
    public R<T> save(@RequestBody ScfInvoiceInfoDTO scfInvoiceInfo) throws Exception {
        return R.status(scfInvoiceInfoService.saveInvoiceInfo(scfInvoiceInfo));
    }

    /**
     * 修改发票信息详情
     */
    @PostMapping("/scfModInvoiceDetails")
    // @CacheEvict(value = "scfInvoiceDetailsCache", key = "'scfInvoiceDetailsCacheKey-' + #scfInvoiceInfo.rowId")
    public R<T> update(@RequestBody ScfInvoiceInfoDTO scfInvoiceInfo) {
        return R.status(scfInvoiceInfoService.updateInvoiceInfo(scfInvoiceInfo));
    }

    /**
     * 批量导入完税（发票）信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfInvoiceBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfInvoiceInfo> scfInvoiceInfos = scfInvoiceInfoService.batchSaveScfInvoiceInfo(operatorId, file);
        int bachSaveInvoiceInfo = scfInvoiceInfoService.bachSaveInvoiceInfo(scfInvoiceInfos, operatorId);
        int total = scfInvoiceInfos.size();
        int totalSussess = bachSaveInvoiceInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }
    @PostMapping("/scfGetInvoice/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfInvoiceInfoService.deleteByIds( split ));
    }
}
